package com.retired_serviceman_management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.retired_serviceman_management.dao.SoldierInfoDao;
import com.retired_serviceman_management.entity.SoldierInfo;
import com.retired_serviceman_management.service.Soldier_Info_Service;
import com.retired_serviceman_management.util.ListUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class Soldier_Info_Service_Impl implements Soldier_Info_Service {
    @Resource
    private SoldierInfoDao soldierInfoDao;

    @Override
    public List<ListUtil> queryAll() {
        List<SoldierInfo> infoList = soldierInfoDao.queryAll();
        if (infoList != null) {
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            List<ListUtil> soldierInfo = new ArrayList<>();
            for (int i = 0; i < infoList.size(); i++) {
                //将退役时间和服役时间转换为指定格式
                String ServiceTime=null;
                String DecommissioningTime=null;
                if(infoList.get(i).getServiceTime()!=null){
                    ServiceTime = sim.format(infoList.get(i).getServiceTime());
                }
               if(infoList.get(i).getDecommissioningTime()!=null){
                   DecommissioningTime = sim.format(infoList.get(i).getDecommissioningTime());
               }

                //将状态值转换为指定文字
                String status = infoList.get(i).getPsStatus();
                if (status!=null&&status.equals("I")) {
                    infoList.get(i).setPsStatus("未就业");
                } else if(status!=null&&status.equals("A")){
                    infoList.get(i).setPsStatus("已就业");
                }
                ListUtil listUtil = new ListUtil(infoList.get(i).getSoldierName(), infoList.get(i).getSoldierAge(), infoList.get(i).getSoldierOrigin(), infoList.get(i).getHomeLocal(), ServiceTime, DecommissioningTime, infoList.get(i).getPsStatus(), infoList.get(i).getMobile());
                soldierInfo.add(listUtil);
            }
            return soldierInfo;
        }
        return null;
    }

    @Override
    public Integer Update_Soldier(ListUtil listUtil) {
        System.out.println("listUtil = " + listUtil);
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date data1 = sim.parse(listUtil.getServiceTime());
            java.sql.Date serviceTime = new java.sql.Date(data1.getTime());
            Date data2 = sim.parse(listUtil.getDecommissioningTime());
            java.sql.Date DecommissioningTime = new java.sql.Date(data2.getTime());
            SoldierInfo soldierInfo = new SoldierInfo();
            soldierInfo.setSoldierName(listUtil.getSoldierName());
            soldierInfo.setSoldierAge(listUtil.getSoldierAge());
            soldierInfo.setSoldierOrigin(listUtil.getSoldierOrigin());
            soldierInfo.setMobile(listUtil.getMobile());
            soldierInfo.setHomeLocal(listUtil.getHomeLocal());
            soldierInfo.setServiceTime(serviceTime);
            soldierInfo.setDecommissioningTime(DecommissioningTime);
            if (listUtil.getPsStatus().equals("未就业")) {
                soldierInfo.setPsStatus("I");
            } else if (listUtil.getPsStatus().equals("已就业")) {
                soldierInfo.setPsStatus("A");
            }


            String ServiceTime1=null;
            String DecommissioningTime1=null;
            SoldierInfo soldierInfo1 = soldierInfoDao.queryLimitByMobile(soldierInfo.getMobile());
            if(soldierInfo1.getServiceTime()!=null) ServiceTime1 = sim.format(soldierInfo1.getServiceTime());
            if(soldierInfo1.getDecommissioningTime()!=null) DecommissioningTime1 = sim.format(soldierInfo1.getDecommissioningTime());
            String status = soldierInfo1.getPsStatus();
            if (status!=null&&status.equals("I")) {
                soldierInfo1.setPsStatus("未就业");
            } else if (status!=null&&status.equals("A")) {
                soldierInfo1.setPsStatus("已就业");
            }
            ListUtil listUtil1 =new ListUtil(soldierInfo1.getSoldierName(), soldierInfo1.getSoldierAge(),soldierInfo1.getSoldierOrigin(),soldierInfo1.getHomeLocal(),ServiceTime1,DecommissioningTime1,soldierInfo1.getPsStatus(),soldierInfo1.getMobile());
            if(listUtil.getSoldierName().equals(listUtil1.getSoldierName())&&listUtil.getSoldierAge().equals(listUtil1.getSoldierAge())&&listUtil.getSoldierOrigin().equals(listUtil1.getSoldierOrigin())&&listUtil.getHomeLocal().equals(listUtil1.getHomeLocal())&&listUtil.getServiceTime().equals(listUtil1.getServiceTime())&&listUtil.getDecommissioningTime().equals(listUtil1.getDecommissioningTime())&&listUtil.getPsStatus().equals(listUtil1.getPsStatus())&&listUtil.getMobile().equals(listUtil1.getMobile())){
                return 2;
            }
            int i = soldierInfoDao.updateByMobile(soldierInfo);
            return i;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer Delete_Soldier(String mobile) {
        Integer i = soldierInfoDao.Delete_Soldier(mobile);
        return i;
    }

    @Override
    public Integer CheckAccount(String mobile, String password) {
        SoldierInfo soldierInfo = soldierInfoDao.queryByMobile(mobile);
        System.out.println("soldierInfo = " + soldierInfo);
        if (soldierInfo == null) {
            return 1;
        } else {
            if (soldierInfo.getPassword().equals(password)) {
                return 0;
            } else return 2;
        }
    }

    @Override
    public ListUtil queryByMobile(String mobile) {
        SoldierInfo soldierInfos = soldierInfoDao.queryByMobile(mobile);
        String ServiceTime;
        String DecommissioningTime;
        if(soldierInfos.getServiceTime()!=null){
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");//将退役时间和服役时间转换为指定格式
            ServiceTime = sim.format(soldierInfos.getServiceTime());
        }else {
            ServiceTime = "2000-01-01";
        }
        if(soldierInfos.getDecommissioningTime()!=null) {
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");//将退役时间和服役时间转换为指定格式
            DecommissioningTime = sim.format(soldierInfos.getDecommissioningTime());
        }else {
            DecommissioningTime = "2000-01-01";
        }
        if (soldierInfos.getPsStatus()!=null&&soldierInfos.getPsStatus().equals("A")) soldierInfos.setPsStatus("已就业");
        else if (soldierInfos.getPsStatus()!=null&&soldierInfos.getPsStatus().equals("I")) soldierInfos.setPsStatus("未就业");
        SoldierInfo soldierInfo = (SoldierInfo) checkObjAllFieldsIsNull(soldierInfos);
        ListUtil listUtil = new ListUtil(soldierInfo.getSoldierName(), soldierInfo.getSoldierAge(), soldierInfo.getSoldierOrigin(), soldierInfo.getHomeLocal(), ServiceTime, DecommissioningTime, soldierInfo.getPsStatus(), soldierInfo.getMobile());
        return listUtil;
    }

    @Override
    public  Integer Update_password(String mobile,String old_password,String new_password,String new_password2){
        if (new_password.equals(new_password2)) {//两次输入的新密码一致
            String password = soldierInfoDao.queryPasswordByMobile(mobile);
            if (password.equals(old_password)) {//输入的旧密码和数据库一致，可以修改
                Integer integer = soldierInfoDao.updatePasswordByMobile(mobile, new_password);
                return integer;//1修改成功  0修改失败
            } else return 2;//输入的旧密码错误
        } else return 3;//两次输入的密码不一致
    }

    @Override
    public Integer register(String soldierName, String mobile, String password, String password1) {
        if (password.equals(password1)) {//两次输入的密码相等
            SoldierInfo soldierInfo1 = soldierInfoDao.queryByMobile(mobile);
            if (soldierInfo1 != null) {
                return 2;//账号已经存在，无法注册
            } else {
                SoldierInfo soldierInfo = new SoldierInfo();
                soldierInfo.setSoldierName(soldierName);
                soldierInfo.setMobile(mobile);
                soldierInfo.setPassword(password);
                int i = soldierInfoDao.insert(soldierInfo);
                return i;//0 失败  1成功
            }
        }
        return 3;//两次输入密码不同
    }

    @Override
    public Integer insert_Soldier(ListUtil listUtil) {
        if (listUtil==null)return 2;//传回的数据为空，重新输入
        SoldierInfo soldierInfo1 = soldierInfoDao.queryByMobile(listUtil.getMobile());
        if(soldierInfo1!=null) return 3;
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date data1 = sim.parse(listUtil.getServiceTime());
            java.sql.Date serviceTime = new java.sql.Date(data1.getTime());
            Date data2 = sim.parse(listUtil.getDecommissioningTime());
            java.sql.Date DecommissioningTime = new java.sql.Date(data2.getTime());//转换日期为数据库类型
            SoldierInfo soldierInfo = new SoldierInfo();
            soldierInfo.setSoldierName(listUtil.getSoldierName());
            soldierInfo.setSoldierAge(listUtil.getSoldierAge());
            soldierInfo.setSoldierOrigin(listUtil.getSoldierOrigin());
            soldierInfo.setMobile(listUtil.getMobile());
            soldierInfo.setHomeLocal(listUtil.getHomeLocal());
            soldierInfo.setServiceTime(serviceTime);
            soldierInfo.setDecommissioningTime(DecommissioningTime);
            soldierInfo.setPassword(listUtil.getPassword());//把listUtil转换为SoldierInfo对象
            if (listUtil.getPsStatus().equals("未就业")) {
                soldierInfo.setPsStatus("I");
            } else if (listUtil.getPsStatus().equals("已就业")) {
                soldierInfo.setPsStatus("A");
            }
            int i = soldierInfoDao.insert(soldierInfo);
            return i;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    public static Object checkObjAllFieldsIsNull(Object object) {
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                System.out.print(f.getName() + ":");
                System.out.println(f.get(object));
                if(f.getName()=="serviceTime"||f.getName()=="decommissioningTime") continue;
                if(f.get(object)==null){
                    f.set(object," ");
                }
            }
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

}
